package com.songlanyun.modules.article.controller.admin;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.songlanyun.common.annotation.SysLog;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.article.model.dto.ArticleDTO;
import com.songlanyun.modules.article.model.vo.ArticleVO;
import com.songlanyun.modules.exception.BbsException;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.songlanyun.modules.article.entity.Article;
import com.songlanyun.modules.article.service.ArticleService;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;

import javax.validation.Validator;


/**
 * 文章表
 *
 * @author wuchao
 * @email sunlightcs@gmail.com
 * @date 2021-02-27 15:36:01
 */
@RestController
@RequestMapping("admin/bbs/article")
public class ArticleAdminController extends AbstractController {
    @Autowired
    private ArticleService articleService;

    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions("article:article:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = articleService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("article:article:info")
    public R info(@PathVariable("id") Long id) {
        ArticleVO vo = articleService.info(id);

        return R.ok().put("data", vo);
    }

    /**
     * 创建文章
     */
    @SysLog("创建文章")
    @PostMapping("/save")
    //@RequiresPermissions("article:article:save")
    public R save(@RequestBody ArticleDTO dto) {
        verifyAdmin();
        ValidatorUtils.validateEntity(dto);
        articleService.saveArticle(dto, getUserId());

        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改文章")
    @PutMapping("/update")
    //@RequiresPermissions("article:article:update")
    public R update(@RequestBody ArticleDTO dto) {
        verifyAdmin();
        ValidatorUtils.validateEntity(dto);
        articleService.updateArticle(dto);

        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除文章")
    @DeleteMapping("/delete/{id}")
    //@RequiresPermissions("article:article:delete")
    public R delete(@PathVariable("id") Long id) {
        verifyAdmin();
        articleService.del(id);
        return R.ok();
    }

    /**
     * 设置或取消推荐文章
     */
    @SysLog("设置或取消推荐文章")
    @PostMapping("/set-recommend")
    //@RequiresPermissions("article:article:save")
    public R setRecommend(@RequestBody Article article) {
        verifyAdmin();
        if (article.getId() == null || article.getIsRecommend() == null) {
            throw new RRException(BbsException.PARAMETER_IS_NOT_COMPLETE);
        }
        //更新推荐状态
        articleService.setRecommend(article.getId(),article.getIsRecommend());

        return R.ok();
    }

    /**
     * 设置上下架
     */
    @SysLog("设置上下架")
    @PostMapping("/added")
    //@RequiresPermissions("article:article:save")
    public R added(@RequestBody Article article) {
        verifyAdmin();
        if (article.getId() == null || article.getAdded() == null) {
            throw new RRException(BbsException.PARAMETER_IS_NOT_COMPLETE);
        }
        //更新上下架状态
        articleService.added(article.getId(),article.getAdded());

        return R.ok();
    }

}
